package com.hhc.controller;

import com.hhc.dto.CourseDTO;
import com.hhc.query.UserQuery;
import com.hhc.service.CourseService;
import com.hhc.utils.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.hhc.dto.CourseQuery;
import jakarta.validation.Valid;

/**
 * <p>
 *   课程信息Controller，处理课程信息相关的HTTP请求
 * </p>
 * @author pc
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 查询课程列表
     *
     * @param query 查询参数
     * @return RestResult
     */
    @GetMapping("/list")
    public RestResult<Object> list(@Valid CourseQuery query) {
        return courseService.list(query);
    }

    /**
     * 新增课程信息
     * @param courseDTO 课程信息DTO
     * @return RestResult结果
     */
    @PostMapping
    public RestResult addCourse(@Validated @RequestBody CourseDTO courseDTO) {
        return courseService.addCourse(courseDTO);
    }

    /**
     * 修改课程信息
     * @param courseDTO 课程信息DTO
     * @return RestResult结果
     */
    @PutMapping
    public RestResult updateCourse(@Validated @RequestBody CourseDTO courseDTO) {
        return courseService.updateCourse(courseDTO);
    }

    /**
     * 删除课程信息
     * @param courseId 课程ID
     * @return RestResult结果
     */
    @DeleteMapping("/{courseId}")
    public RestResult deleteCourse(@PathVariable Long courseId) {
        return courseService.deleteCourse(courseId);
    }

    /**
     * 更改课程状态
     * @param courseId 课程ID
     * @param status 课程状态
     * @return RestResult结果
     */
    @PatchMapping("/{courseId}/status")
    public RestResult changeCourseStatus(@PathVariable Long courseId, @RequestParam Integer status) {
        return courseService.changeCourseStatus(courseId, status);
    }

    @GetMapping("/available")
    public RestResult<List<CourseDTO>> getAvailableCourseList(@Valid @ModelAttribute CourseQuery query) {
        return courseService.getAvailableCourseList(query);
    }

    @PostMapping("/enroll")
    public RestResult<String> enrollCourse(@RequestParam Long courseId) {
        return courseService.enrollCourse(courseId);
    }

    @GetMapping("/enrolled")
    public RestResult<List<CourseDTO>> getEnrolledCourseList(@Valid @ModelAttribute CourseQuery query) {
        return courseService.getEnrolledCourseList(query);
    }

    @PostMapping("/withdraw")
    public RestResult<String> withdrawCourse(@RequestParam Long courseId) {
        return courseService.withdrawCourse(courseId);
    }
}